* This file codes the ANES cumulative file for those analyses in the main text 
* 	and appendix

use "/Users/jrthornton/Desktop/POBE 2021 replication/Data/anes_timeseries_cdf.dta" , replace
graph drop _all

* year of study
gen year = VCF0004
drop if year<2005

svyset [pw=VCF0009z]

set more off

******************
* SOPHISTICATION *
******************
* Created with three items: knowledge, interest, and involvement
		
* Interviewer Knowledge
gen interviewer_assessment = VCF0050a
recode interviewer_assessment (9=.) 	(1=5) (5=1) (2=4) (4=2)

* Interest
gen interest = VCF0310
recode interest (9=.) (0=.)

* Involvement 
gen involvement= VCF0723
recode involvement (0=.)

* Knowledge of house
gen house_before  = VCF0729
recode house_before (1=0) (2=1) (0=.)

* Scale from IRT with interviewer assessment
irt grm interviewer_assessment  interest  involvement if year>2005
predict soph_irt, latent
replace soph_irt = . if interviewer_assessment==.
replace soph_irt = . if interest == .
replace soph_irt = . if involvement == .
su soph_irt, meanonly
replace soph_irt = (soph_irt- r(min))/(r(max) - r(min))

* Split sample into thirds:
xtile soph_cat = soph_irt, nq(3)

* Alternate scale from IRT replaceing interviewer_assessment with knowledge of House
 irt hybrid (grm interest involvement ) (1pl house)
predict soph_irt_alt, latent
replace soph_irt_alt = . if house_before==.
replace soph_irt_alt = . if interest == .
replace soph_irt_alt = . if involvement == .
su soph_irt_alt, meanonly
replace soph_irt_alt = (soph_irt_alt- r(min))/(r(max) - r(min))
xtile soph_cat_alt = soph_irt_alt, nq(3)

* Four item measure:
irt hybrid (grm interest involvement interviewer_assess) (1pl house)
predict soph_irt_alt2, latent
replace soph_irt_alt2 = . if house_before==.
replace soph_irt_alt2 = . if interest == .
replace soph_irt_alt2 = . if involvement == .
su soph_irt_alt2, meanonly
replace soph_irt_alt2 = (soph_irt_alt2- r(min))/(r(max) - r(min))

* Three item additive index of main scale
su interviewer_assessment, meanonly
replace interviewer_assessment = (interviewer_assessment- r(min)) ///
	/(r(max) - r(min))
su interest, meanonly
replace interest = (interest- r(min))/(r(max) - r(min))
su involvement, meanonly
replace involvement = (involvement- r(min))/(r(max) - r(min))

pca interviewer_assessment  interest  involvement
alpha interviewer_assessment  interest  involvement
predict d1
gen sophistication = (interviewer_assessment + interest + involvement)/3
gen sophistication_alt = (house_before + interest + involvement)/3


gen speaker = VCF9260	// What job or office is held by named Speaker of the U.S. House
recode speaker (2=0)

gen which_party = VCF0502	// Which Major Party is More Conservative 3-Category
recode which_party (2=0) (9=.)

gen sc_justice = VCF9262	// What job or office is held by named Chief Justice of the U.S. Supreme Court
recode sc_justice (2=0)

gen vp = VCF9261	// What job or office is held by named Vice-President
recode vp (2=0)

irt 2pl speaker which_party sc_justice vp
predict info_irt, latent 
su info_irt, meanonly
replace info_irt = (info_irt- r(min)) /(r(max) - r(min))




*******************
* EQUALITARIANISM *
*******************
* Scale created from four tiems
	* 1. 'Our society should do whatever is necessary to make sure that 
			* everyone has an equal opportunity to succeed.'
	* 2 (not asked in 2016). 'We have gone too far in pushing equal rights in this country.'
	* 3 (not asked in 2016). 'One of the big problems in this country is that we don't give
			* everyone an equal chance.'
	* 4. 'It is not really that big a problem if some people have more of a
			* chance in life than others.'
	* 5. 'The (2004: This_ country would be better off if we worried less
			* about how equal people are.'		
	* 6. 'If people were treated more equally in this country we would have
			* many fewer problems.'

gen equality1 = VCF9013
recode equality1 (8=.) (9=.)
recode equality1 (1=5) (5=1) (2=4) (4=2)

gen equality2 = VCF9014 // not asked in 2016
recode equality2 (8=.) (9=.)

gen equality3 = VCF9015 // not asked in 2016
recode equality3 (8=.) (9=.)
recode equality3 (1=5) (5=1) (2=4) (4=2)

gen equality4 = VCF9016
recode equality4 (8=.) (9=.)

gen equality5 = VCF9017
recode equality5 (8=.) (9=.)

gen equality6 = VCF9018
recode equality6 (8=.) (9=.)
recode equality6 (1=5) (5=1) (2=4) (4=2)


gen equality = (equality1 + equality4 + equality5 ///
	+ equality6)/4
su equality, meanonly
replace equality = (equality- r(min)) /(r(max) - r(min))

egen equality_constraint = rsd(equality1 equality2 equality3 equality4 equality5 equality6) 
replace equality_constraint = -1 * equality_constraint


su equality_constraint, meanonly
replace equality_constraint = (equality_constraint - r(min)) ///
/(r(max) - r(min))


gen missing = 0
gen missing1 = 1 if equality1==.
replace missing1 = 0 if equality1!=.
*gen missing2 = 1 if equality2==.
*replace missing2 = 0 if equality2!=.
*gen missing3 = 1 if equality3==.
*replace missing3 = 0 if equality3!=.
gen missing4 = 1 if equality4==.
replace missing4 = 0 if equality4!=.
gen missing5 = 1 if equality5==.
replace missing5 = 0 if equality5!=.
gen missing6 = 1 if equality6==.
replace missing6 = 0 if equality6!=.
replace missing = missing1+missing4+missing5+missing6

irt grm equality1  equality4 - equality6
predict equality_irt, latent 
replace equality_irt = . if missing >0
su equality_irt, meanonly
replace equality_irt = (equality_irt- r(min)) /(r(max) - r(min))

irt grm equality1  equality4 - equality6 if soph_cat!=., group(soph_cat)
predict equality_dif, latent 
replace equality_dif = . if missing >0
su equality_dif, meanonly
replace equality_dif = (equality_dif- r(min)) /(r(max) - r(min))

*2008 and 2012 six-item scale
irt grm equality1 - equality6 if year!=2016
predict equality_irt_alt if year!=2016, latent 
sum equality_irt_alt, meanonly
replace equality_irt_alt = (equality_irt_alt- r(min)) /(r(max) - r(min)) 
alpha equality1 - equality6

pca equality1 equality4 - equality6  
alpha equality1 equality4 - equality6  


*****************************************
* ISSUES RELATED TO GROUPS AND EQUALITY *
*****************************************
*********************
* African Americans *
*********************
*ISSUES: Aid to Blacks Scale
*	Some people say that because of past discrimination blacks should be
*		given preference in hiring and promotion.  Others say that such
*		preference in hiring and promotion of blacks is wrong because it gives
*		blacks advantages they haven't earned.  What about your opinion-- are
*		you for or against preferential hiring and promotion of blacks?

gen aid_to_blacks = VCF0830	
recode aid_to_blacks (0=.) (9=.)
revrs aid_to_black, replace	

*ISSUES: Affirmative Action in Hiring/Promotion
*	Some people say that because of past discrimination blacks should be
*		given preference in hiring and promotion.  Others say that such
*		preference in hiring and promotion of blacks is wrong because it gives
*		blacks advantages they haven't earned.  What about your opinion-- are
*		you for or against preferential hiring and promotion of blacks?

gen affirmative_action = VCF0867a		
recode affirmative_action (8=3) (7=3) (9=.)	
revrs affirmative_action, replace	


*ISSUES: Government Should Ensure Fair Jobs for Blacks
	* Only available in 1986 and 1988
gen fair_jobs = VCF9038	
recode fair_jobs (8=3.) (9=.)	
su fair_jobs, meanonly
replace fair_jobs = (fair_jobs- r(min))/(r(max) - r(min))
	
* Scale for aid to blacks/affirmative action:
set more off 
irt grm aid_to_blacks  affirmative_action , iterate(200)
predict black_issues_irt, latent 
replace black_issues_irt  = . if aid_to_blacks==.
replace black_issues_irt  = . if affirmative_action==.
su black_issues_irt, meanonly
replace black_issues_irt  = (black_issues_irt- r(min)) /(r(max) - r(min))

su aid_to_blacks, meanonly
replace aid_to_blacks = (aid_to_blacks- r(min))/(r(max) - r(min))
su affirmative_action, meanonly
replace affirmative_action = (affirmative_action- r(min))/(r(max) - r(min))
gen black_issues = (aid_to_blacks + affirmative_action) / 2
alpha aid_to_blacks affirmative_action

 alpha affirm aid_to_blacks 
 
************************************************
* Poor Americans – welfare and aid to the poor *
************************************************
gen welfare = VCF0894
recode welfare (8=.) (9=.)
revrs welfare , replace

gen aid_to_poor = VCF0886
recode aid_to_poor (8=.) (9=.)
revrs aid_to_poor , replace

* Scale for welfare/aid to the poor:
alpha welfare aid_to_poor

irt grm welfare  aid_to_poor //, iterate(200)
predict welfare_support_irt, latent 
replace welfare_support_irt = . if welfare==.
replace welfare_support_irt = . if aid_to_poor==.
su welfare_support_irt, meanonly
replace welfare_support_irt  = (welfare_support_irt- r(min)) /(r(max) - r(min))

gen welfare_support = (welfare + aid_to_poor)/2
sum welfare_support, meanonly
replace welfare_support = (welfare_support- r(min)) ///
	/(r(max) - r(min))
	

**********************************************
* LGBTQ – discrimination, military, adoption *
**********************************************
*ISSUES: Strength of Position on Law to Protect Homosexuals Against Discrimination
gen gay_discrimination = VCF0876a	
recode gay_discrimination (9=.) (8=.) (7=.) (1=4) (2=3) (4=2) (5=1)

*ISSUES: Gay military
gen gay_military = VCF0877a	
recode gay_military (9=.) (8=.) (7=.) (1=4) (2=3) (4=2) (5=1)

*ISSUES: Should Gays/Lesbians Be Able to Adopt Children
gen gay_adopt = VCF0878	
recode gay_adopt (9=.) (8=.)	
recode gay_adopt (5=0)

set more off
irt hybrid (grm gay_discrimination gay_military) (2pl gay_adopt) 
predict gay_irt, latent
replace gay_irt = . if gay_discrimination==.
replace gay_irt = . if gay_military==.
replace gay_irt = . if gay_adopt==.
su gay_irt, meanonly
replace gay_irt  = (gay_irt- r(min)) /(r(max) - r(min))

factor gay_discrimination gay_military gay_adopt , ipf
predict f1

su gay_discrimination, meanonly
replace gay_discrimination  = (gay_discrimination- r(min)) /(r(max) - r(min))
su gay_military, meanonly
replace gay_military  = (gay_military- r(min)) /(r(max) - r(min))
gen gay_issues = (gay_discrimination + gay_military + gay_adopt)/3
alpha gay_discrimination gay_military gay_adopt


*******************
* GROUP VARIABLES *
*******************
* Race
gen race = VCF0105b
recode race (0=.) (9=.)
label define racelabel 1 "White" 2 "Black" 3 "Hispanic" 4 "Other" 
label values race racelabel  
*1. White non-Hispanic
*2. Black non-Hispanic
*3. Hispanic
*4. Other or multiple races, non-Hispanic

* Gender
gen sex = VCF0104
recode sex (0=.) (3=.)
replace sex = sex-1
label define sexlabel 0 "Male" 1 "Female" 
label values sex sexlabel

gen categories = .
replace categories = 1 if sex == 1 & race==2
replace categories = 2 if sex == 1 & race==1
replace categories = 3 if sex == 0 & race==2
replace categories = 4 if sex == 0 & race==1
# delimit ; 
label define cat_label        
	1 "Black woman" 
	1 "Black woman" 
	2 "White woman" 
	3 "Black man" 
	4 "White man"          
;
# delimit cr
label values categories cat_label


*********************
* CONTROL VARIABLES *
*********************
gen mode = VCF0017
recode mode (4=1)

gen age = VCF0101
replace age = 97 if age>97

gen educ = VCF0140
replace educ = . if educ >6
recode educ (1=1) (2=2) (3=3) (4=3) (5=4) (6=5)

gen income = VCF0114
replace income = . if VCF0114 == 0
label define incomelabel 1 "0 to 16 percentile" 2 "17 to 33 percentile" 3 "34 to 67 percentile" 4 "68 to 95 percentile" 5 "96 to 100 percentile"
label values income incomelabel  
*1. 0 to 16 percentile
*2. 17 to 33 percentile
*3. 34 to 67 percentile
*4. 68 to 95 percentile
*5. 96 to 100 percentile

* Ideology
gen ideology = VCF0803
recode ideology (9=.) (0=.)

* Partisanship
gen pid = VCF0301
recode pid (0=.)
# delimit ; 
label define pid_label        
		1 "Strong Democrat" 
        2 "Weak Democrat" 
        3 "Independent - Democrat" 
        4 "Independent - Independent" 
        5 "Independent - Republican" 
		6 "Weak Republican" 
        7 "Strong Republican"
;
# delimit cr
label values pid pid_label

gen party = 0 if pid<4
replace party = 1 if pid==4
replace party = 2 if pid>4
label define partylabel 0 "Democrat" 1 "Independent" 2 "Republican" 
label values party partylabel


gen pidi = abs(pid-4)
# delimit ; 
label define pidi_label        
		0 "Independent" 
		1 "Weak partisan" 
        2 "Partisan" 
		3 "Strong partisan" 
;
# delimit cr
label values pidi pidi_label

gen democrat = 1 if pid<4

* Codes racial resentment for Table E3
*RACIAL RESENTMENT: Conditions Make it Difficult for Blacks to Succeed
gen racial_resentment1 = VCF9039	
recode racial_resentment1 (8=.) (9=.)
recode racial_resentment1 (1=5) (5=1) (4=2) (2=4)

*RACIAL RESENTMENT: Blacks Should Not Have Special Favors to Succeed
gen racial_resentment2 = VCF9040	
recode racial_resentment2 (8=.) (9=.)

*RACIAL RESENTMENT: Blacks Must Try Harder to Succeed
gen racial_resentment3 = VCF9041	
recode racial_resentment3 (8=.) (9=.)

*RACIALRESENTMENT: Blacks Gotten Less than They Deserve Over the Past Few Years
* Higher values represent more resentment; coded to range from zero to one
gen racial_resentment4 = VCF9042	
recode racial_resentment4 (8=.) (9=.)
recode racial_resentment4 (1=5) (5=1) (4=2) (2=4)

alpha racial_resentment1 racial_resentment2 racial_resentment3 racial_resentment4 

gen racial_resentment = (racial_resentment1 + racial_resentment2 + ///
	racial_resentment3 + racial_resentment4)/4

revrs racial_resentment , replace
su racial_resentment, meanonly
replace racial_resentment = (racial_resentment- r(min)) ///
	/(r(max) - r(min))


irt grm racial_resentment1 - racial_resentment4 if year>2005
predict racial_resentment_irt, latent
revrs racial_resentment_irt , replace
su racial_resentment_irt, meanonly
replace racial_resentment_irt = (racial_resentment_irt- r(min)) ///
	/(r(max) - r(min))
replace racial_resentment_irt=. if racial_resentment1==.
replace racial_resentment_irt=. if racial_resentment4==.
replace racial_resentment_irt=. if racial_resentment4==.
replace racial_resentment_irt=. if racial_resentment4==.

* Blacks lazy
gen blackslazy = VCF9271

* Whites lazy (reverse coded)
gen whiteslazy = VCF9270
*revrs whiteslazy , replace

* Hispanics lazy (reverse coded)
gen latinoslazy = VCF9272
*revrs latinoslazy , replace

gen prejudice = blackslazy-whiteslazy if race == 1
replace prejudice = blackslazy-latinoslazy if race == 3

* Black termometer
gen blacktherm = VCF0206
recode blacktherm (97=100)
*White thermometer
gen whitetherm = VCF0207
recode whitetherm (97=100)

*White thermometer
gen latinotherm = VCF0217
recode latinotherm (97=100)

* Difference
gen diftherm = whitetherm-blacktherm if race==1
replace diftherm = latinotherm-blacktherm if race==3

* Social security 
gen social_security = VCF9049
recode social_security (7=.) (8=.) (1=3) (3=1)

* Moral traditionalism
gen newlifestyles = VCF0851 if VCF0851!=8 & VCF0851!=9
gen adjustmorals = VCF0852 if VCF0852!=8 & VCF0852!=9
gen traditionalvalues = VCF0853 if VCF0853!=8 & VCF0853!=9
gen moraltolerance = VCF0854 if VCF0854!=8 & VCF0854!=9

irt grm newlifestyles adjustmorals traditionalvalues moraltolerance if year>2005
predict tradvalues, latent
su tradvalues, meanonly
replace tradvalues = (tradvalues- r(min)) ///
	/(r(max) - r(min))
replace tradvalues=. if newlifestyles==.
replace tradvalues=. if adjustmorals==.
replace tradvalues=. if traditionalvalues==.
replace tradvalues=. if moraltolerance==.
 
* Church attendance
gen church = VCF0130
replace church = . if church > 5
replace church = . if church < 1
recode church (5=1) (4=2) (3=3) (2=4) (1=5)


* Evangelical
gen evangelical=0 
replace evangelical=1 if VCF0128b==1 | VCF0129==123 | VCF0129==122 | /// 
VCF0129==136 | VCF0129==131 | VCF0129==135 | VCF0129==131 | VCF0129==123 | ///
VCF0129==135 | VCF0129==123 | VCF0129==139 | ///
VCF0152==100 | VCF0152==102 | VCF0152==109 | ///
VCF0152==120 | VCF0152==122 | VCF0152==123 | VCF0152==124 | VCF0152==125 | ///
VCF0152==126 | VCF0152==127 | VCF0152==128 | VCF0152==135 | VCF0152==147 | ///
VCF0152==148 | VCF0152==149 | VCF0152==160 | VCF0152==161 | VCF0152==162 | ///
VCF0152==166 | VCF0152==167 | VCF0152==168 | VCF0152==170 | VCF0152==180 | ///
VCF0152==181 | VCF0152==182 | VCF0152==183 | VCF0152==184 | VCF0152==185 | ///
VCF0152==186 | VCF0152==199 | VCF0152==200 | VCF0152==201 | VCF0152==219 | ///
VCF0152==221 | VCF0152==222 | VCF0152==223 | VCF0152==230 | ///
VCF0152==234 | VCF0152==240 | VCF0152==250 | VCF0152==251 | VCF0152==252 | ///
VCF0152==253 | VCF0152==254 | VCF0152==255 | VCF0152==256 | VCF0152==257 | ///
VCF0152==258 | VCF0152==260 | VCF0152==261 | VCF0152==262 | VCF0152==263 | ///
VCF0152==264 | VCF0152==267 | VCF0152==268 | VCF0152==269 | VCF0152==272 | ///
VCF0152==275 | VCF0152==291 | VCF0152==292 | VCF0152==306 

gen evangelical2 = VCF9243
recode evangelical2 (2=0)


save "/Users/jrthornton/Desktop/POBE 2021 replication/Data/Cumulative file coded.dta" , replace

* Other policies (Appendix I) 
gen environment = VCF9047 if VCF9047<7
revrs environ, replace

gen childcare = VCF0887 if VCF9047<7
replace childcare = . if childcare>3
revrs childcare, replace


gen selfinsure = VCF0806
replace selfinsure = . if selfinsure < 1
replace selfinsure = . if selfinsure >= 8
recode selfinsure (1=-3) (2=-2) (3=-1) (4=0) (5=1) (6=2) (7=3)
revrs selfinsure , replace
label define insurelab 1 "Government insurance plan" 7 "Private insurance plan"
label values selfinsure insurelab

gen selfservice = VCF0839
replace selfservice = . if selfservice < 1
replace selfservice = . if selfservice >= 8
label define servicelab 1 "Government should provide many more services" ///
	7 "Government should provide many ewer services"
label values selfservice servicelab


gen guarjobs = VCF0809 if VCF0809<8
revrs guarjobs, replace

gen schools = VCF0890 if VCF0890<7
revrs schools, replace


* Rescale 0-1
foreach v of var environment schools selfinsure selfservice guarjobs childcare { 
	su `v', meanonly 
	gen `v'2 = (`v' - r(min))/(r(max) - r(min)) 
}


save "/Users/jrthornton/Desktop/POBE 2021 replication/Data/Cumulative file coded.dta" , replace




* Export IRT results for density plot in R:
use "/Users/jrthornton/Desktop/POBE 2021 replication/Data/Cumulative file coded.dta" , replace
drop if year<2005
drop if equality_irt == .
drop if soph_cat==.
drop if income==.
drop if race==.
gen n = 1
saveold "/Users/jrthornton/Desktop/POBE 2021 replication/Data/ANES_irt_all.dta" , version(12) replace


use "/Users/jrthornton/Desktop/POBE 2021 replication/Data/Cumulative file coded.dta" , replace
